package map.hashmap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.util.CollectionUtils;

public class HashMapMethodTest {

    public static void main(String[] args) {
        Map<KeyC, ValueC> map = new HashMap<>();
        KeyC key1 = new KeyC(1);
        KeyC key2 = new KeyC(2);
        ValueC v1 = new ValueC("1111");
        ValueC v2 = new ValueC("2222");
        map.put(key1, v1);
        map.put(key2, v2);
        
        Handle handle = new Handle();
        // replaceAll方法测试
//        map.replaceAll(handle::replaceAllOpt);
        map.replaceAll(KeyC::replaceAllOpt);
        map.forEach((k,v) -> {System.out.println(k+":"+v);});
        System.out.println("------------------------");

        // forEach方法测试
        map.forEach(KeyC::handKV);
        map.forEach((k,v) -> {System.out.print(k+"------------");System.out.println(v);});
        map.forEach(handle::forEachOpt);
        handle.getKeys().forEach(System.out::println);

        // merge 方法测试
        ValueC newV = new ValueC("+++");
        ValueC valueResult = map.merge(key1, newV, handle::mergeValue);
        // System.out.println(valueResult);
        // System.out.println(map.get(key1));
        newV = new ValueC("---");
        valueResult = map.merge(key2, newV, ValueC::mergeValue);
        // System.out.println(valueResult);
        // System.out.println(map.get(key2));

    }

}

class KeyC {
    private Integer key;
    public KeyC(int key) {
        this.key = key;
    }
    public Integer getKey() {
        return key;
    }
    public void setKey(Integer key) {
        this.key = key;
    }
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("KeyC [key=");
        builder.append(key);
        builder.append("]");
        return builder.toString();
    }
    public void handKV(ValueC v) {
        System.out.println(this + "——" + v);// 两个toString拼接
    }
    public ValueC replaceAllOpt(ValueC v) {
        System.out.println("after-replace-key——" + this);
        return new ValueC("new:" + v.getValue());
    }
}
class ValueC {
    private String value;
    public ValueC(String value) {
        this.value = value;
    }
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("ValueC [value=");
        builder.append(value);
        builder.append("]");
        return builder.toString();
    }
    public ValueC mergeValue(ValueC v) {
        return new ValueC(
                this.getValue() + v.getValue() + ":after-merge-value");
    }
}
class Handle {
    private List<KeyC> keys;

    public ValueC mergeValue(ValueC v1, ValueC v2) {
        return new ValueC("after-merge:" + v1.getValue() + v2.getValue());
    }

    public void forEachOpt(KeyC key, ValueC v) {
        System.out.println("forEachOpt——" + key + ":" + v);
        if (CollectionUtils.isEmpty(this.keys)) {
            this.keys = new ArrayList<>();
            System.out.println("keys initet");
        }
        this.keys.add(key);
    }

    public List<KeyC> getKeys() {
        return this.keys;
    }
    public ValueC replaceAllOpt(KeyC k, ValueC v) {
        System.out.println("after-replace-key——" + k);
        return new ValueC("new:" + v.getValue());
    }
}